package com.example.springpethospital.util;

import java.util.*;

public class Utility {
    public static Map<String, Object> newResponseObject(boolean success, Object data) {
        Map<String, Object> ret = new HashMap<>();
        ret.put("success", success);
        ret.put("data", data);
        return ret;
    }

    public static Map<String, Object> newResponseObject(boolean success) {
        Map<String, Object> ret = new HashMap<>();
        ret.put("success", success);
        ret.put("data", new HashMap<>());
        return ret;
    }

    public static Map<String, Object> newResponseObject(String errorMsg) {
        Map<String, Object> dat = new HashMap<>();
        dat.put("message", errorMsg);
        return newResponseObject(false, dat);
    }

    public static Map<String, Object> newResponseObject(boolean success, int totalpages, Object data) {
        Map<String, Object> ret = new HashMap<>();
        ret.put("success", success);
        ret.put("totalpages", totalpages);
        ret.put("data", data);
        return ret;
    }

    public static String getRenamedFileName(String oriFileName) {
        if (oriFileName != null && !oriFileName.isEmpty()) {
            String[] afilenames = oriFileName.split("\\.");
            List<String> nFileName = new LinkedList<>(Arrays.asList(afilenames));
            nFileName.add(nFileName.size() - 1, Long.toString(new Date().getTime()));   //文件末尾加上时间戳，防重复
            StringBuilder newFileName = new StringBuilder();
            for (int i = 0; i < nFileName.size(); i++) {
                newFileName.append(nFileName.get(i));
                if (i != nFileName.size() - 1) {
                    if (i == nFileName.size() - 3) newFileName.append('_');
                    else newFileName.append('.');
                }
            }
            return newFileName.toString();
        } else {
            return null;
        }
    }
}